package com.wyzz.hr.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.hibernate.IHibernateUtil;
import com.wyzz.hr.spring.SpringBeanFactory;

public class Pager {

	public Pager() {
		order = "";
		operate = "";
		totalRows = 0;
		totalPages = 0;
		pageSize = 20;
		currentPage = 1;
		isSplit = true;
		hasPrevious = false;
		hasNext = false;
	}

	public int getPageSizeFromCookie() {
		HttpServletRequest request = ServletActionContext.getRequest();
		if (request == null)
			return pageSize;
		Cookie cookies[] = request.getCookies();
		for (int i = 0; i < cookies.length; i++) {
			Cookie c = cookies[i];
			if (c.getName().equalsIgnoreCase("pageSize"))
				return Integer.parseInt(c.getValue());
		}

		return pageSize;
	}

	public void init(int totalRows, int pageSize) {
		this.pageSize = pageSize;
		if (pageSize < 0)
			pageSize = 0x5f5e0ff;
		this.totalRows = totalRows;
		totalPages = ((totalRows + pageSize) - 1) / pageSize;
		start = currentPage - 4 <= 0 ? 1 : currentPage - 4;
		end = currentPage + 5 >= totalPages ? totalPages : currentPage + 5;
		if (end == totalPages)
			start = totalPages - 9 > 0 ? totalPages - 9 : 1;
		else if (start == 1 && end < totalPages)
			end = totalPages <= 10 ? totalPages : 10;
		if (currentPage > totalPages)
			currentPage = totalPages;
		if (currentPage <= 0)
			currentPage = 1;
	}

	public void splitPage(DetachedCriteria detachedCriteria) {
		IHibernateUtil dao = (IHibernateUtil) SpringBeanFactory.getBean("dao");
		int size = dao.findRowCountByCriteria(detachedCriteria);
		int pageSize = getPageSizeFromCookie();
		detachedCriteria.setProjection(null);
		init(size, pageSize);
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public void first() {
		currentPage = 1;
		setHasPrevious(false);
		refresh();
	}

	public void previous() {
		currentPage--;
		operate = "";
		refresh();
	}

	public void next() {
		if (currentPage < totalPages)
			currentPage++;
		operate = "";
		refresh();
	}

	public void last() {
		currentPage = totalPages;
		setHasNext(false);
		refresh();
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public void refresh() {
		if (currentPage > totalPages)
			currentPage = totalPages;
		if (currentPage < 1)
			currentPage = 1;
		if (totalPages <= 1) {
			hasPrevious = false;
			hasNext = false;
		} else if (currentPage == 1) {
			hasPrevious = false;
			hasNext = true;
		} else if (currentPage == totalPages) {
			hasPrevious = true;
			hasNext = false;
		} else {
			hasPrevious = true;
			hasNext = true;
		}
		if (getTotalRows() <= getPageSize())
			setSplit(false);
		else
			setSplit(true);
	}

	public boolean isSplit() {
		return isSplit;
	}

	public void setSplit(boolean isSplit) {
		this.isSplit = isSplit;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	private String order;
	private String operate;
	private int totalRows;
	private int totalPages;
	private int pageSize;
	private int currentPage;
	private boolean isSplit;
	private boolean hasPrevious;
	private boolean hasNext;
	private int start;
	private int end;
}
